/*********************************************************************************************************************
 * COPYRIGHT NOTICE
 * Copyright (c) 2018,逐飞科技
 * All rights reserved.
 * 技术讨论QQ群：	一群：179029047(已满)
 *					二群：244861897(已满)
 *					三群：824575535
 *
 * 以下所有内容版权均属逐飞科技所有，未经允许不得用于商业用途，
 * 欢迎各位使用并传播本程序，修改内容时必须保留逐飞科技的版权声明。
 *
 * @file			seekfree_ft6336.c
 * @company			成都逐飞科技有限公司
 * @author			逐飞科技(QQ3184284598)
 * @version			查看doc内version文件 版本说明
 * @Software		IAR 8.3 or MDK 5.24
 * @Taobao			https://seekfree.taobao.com/
 * @date			2020-03-25
 * @note			本文件作为IOT开发板 seekfree_ft6336 传感器API
 ********************************************************************************************************************/
#include "seekfree_ft6336.h"

ftf6336_touch_point_ref_t ftf6336_tpr_structure;
static const uint16_t ft6336_tpx_tbl[5] =
{
	FT6336_FT_TP1_REG, FT6336_FT_TP2_REG, FT6336_FT_TP3_REG, FT6336_FT_TP4_REG, FT6336_FT_TP5_REG
};

//-------------------------------------------------------------------------------------------------------------------
//  @brief      FT6236 初始化
//  @param      Index			使用的 FlexCOMM 端口
//  @return     void					
//  @since      v1.0
//-------------------------------------------------------------------------------------------------------------------
#ifdef IOT_BOARD
void ft6336_init (void)
{
	uint8_t databuffer[2];
	memset((char *)databuffer, 0, 2);

	iot_arduino_iic_master_default_init();

	iot_arduino_iic_read_reg(FT6336_FT_CMD_RD>>1, FT6336_ID_G, databuffer, 1);
	iot_arduino_iic_write_reg(FT6336_FT_CMD_RD>>1, FT6336_FT_DEVIDE_MODE, 0);
	iot_arduino_iic_write_reg(FT6336_FT_CMD_RD>>1, FT6336_FT_ID_G_THGROUP, 22);
	iot_arduino_iic_read_reg(FT6336_FT_CMD_RD>>1, FT6336_FT_ID_G_THGROUP, databuffer, 1);
	iot_arduino_iic_write_reg(FT6336_FT_CMD_RD>>1, FT6336_FT_ID_G_PERIODACTIVE, 12);
}
#else
void ft6336_init (flexcomm_index_list Index)
{
	uint8_t databuffer[2];
	i2c_master_config_t iic_ft6336_config;

	memset((char *)databuffer, 0, 2);
	zf_iic_master_get_default_config( &iic_ft6336_config);
	zf_flexcomm_init(Index, FLEX_COMM_IIC_MASTER, &iic_ft6336_config);

	zf_iic_read_reg(Index, FT6336_FT_CMD_RD>>1, FT6336_ID_G, databuffer, 1);
	zf_iic_write_reg(Index, FT6336_FT_CMD_RD>>1, FT6336_FT_DEVIDE_MODE, 0);
	zf_iic_write_reg(Index, FT6336_FT_CMD_RD>>1, FT6336_FT_ID_G_THGROUP, 22);
	zf_iic_read_reg(Index, FT6336_FT_CMD_RD>>1, FT6336_FT_ID_G_THGROUP, databuffer, 1);
	zf_iic_write_reg(Index, FT6336_FT_CMD_RD>>1, FT6336_FT_ID_G_PERIODACTIVE, 12);
}
#endif

//-------------------------------------------------------------------------------------------------------------------
//  @brief      FT6236 扫描
//  @param      Index			使用的 FlexCOMM 端口
//  @return     void					
//  @since      v1.0
//-------------------------------------------------------------------------------------------------------------------
#ifdef IOT_BOARD
void ft6336_scan (void)
{
	uint8_t i=0;
	uint8_t sta = 0;
	uint8_t buf[4] = {0, 0, 0, 0};    

    iot_arduino_iic_read_reg(FT6336_FT_CMD_RD>>1, 0x02, &sta, 1);
 	if(sta & 0x0f)	//判断是否有触摸点按下，0x02寄存器的低4位表示有效触点个数
 	{
 		ftf6336_tpr_structure.touchsta.all = ~(0xFF << (sta & 0x0F));	//~(0xFF << (sta & 0x0F))将点的个数转换为触摸点按下有效标志
 		for(i=0;i<5;i++)	                                //分别判断触摸点1-5是否被按下
 		{
 			if(ftf6336_tpr_structure.touchsta.all & (1<<i))			    //读取触摸点坐标
 			{											    //被按下则读取对应触摸点坐标数据
 				//FT6236_RD_Reg(FT6236_TPX_TBL[i],buf,4);	//读取XY坐标值
				iot_arduino_iic_read_reg(FT6336_FT_CMD_RD>>1, ft6336_tpx_tbl[i], buf, 4);

				ftf6336_tpr_structure.x[i]=((uint16_t)(buf[0]&0X0F)<<8)+buf[1];
				ftf6336_tpr_structure.y[i]=((uint16_t)(buf[2]&0X0F)<<8)+buf[3];
 				if((buf[0]&0XC0)!=0X80)
 				{
					ftf6336_tpr_structure.x[i]=ftf6336_tpr_structure.y[i]=0;//必须是contact事件，才认为有效	
					return;
				}
 			}
 		}
 		ftf6336_tpr_structure.touchsta.all |= FT6336_TP_PRES_DOWN;     //触摸按下标记
 	}
 	else
 	{
 		if(ftf6336_tpr_structure.touchsta.all & FT6336_TP_PRES_DOWN) 	//之前是被按下的
 			ftf6336_tpr_structure.touchsta.all &= ~0x80;        //触摸松开标记	
 		else
 		{
 			ftf6336_tpr_structure.x[0] = 0;
 			ftf6336_tpr_structure.y[0] = 0;
 			ftf6336_tpr_structure.touchsta.all &=0xe0;	//清楚触摸点有效标记
 		}
 	}
}
#else
void ft6336_scan (flexcomm_index_list Index)
{
	uint8_t i=0;
	uint8_t sta = 0;
	uint8_t buf[4] = {0, 0, 0, 0};    

    zf_iic_read_reg(Index, FT6336_FT_CMD_RD>>1, 0x02, &sta, 1);
 	if(sta & 0x0f)	//判断是否有触摸点按下，0x02寄存器的低4位表示有效触点个数
 	{
 		ftf6336_tpr_structure.touchsta.all = ~(0xFF << (sta & 0x0F));	//~(0xFF << (sta & 0x0F))将点的个数转换为触摸点按下有效标志
 		for(i=0;i<5;i++)	                                //分别判断触摸点1-5是否被按下
 		{
 			if(ftf6336_tpr_structure.touchsta.all & (1<<i))			    //读取触摸点坐标
 			{											    //被按下则读取对应触摸点坐标数据
 				//FT6236_RD_Reg(FT6236_TPX_TBL[i],buf,4);	//读取XY坐标值
				zf_iic_read_reg(Index, FT6336_FT_CMD_RD>>1, ft6336_tpx_tbl[i], buf, 4);

				ftf6336_tpr_structure.x[i]=((uint16_t)(buf[0]&0X0F)<<8)+buf[1];
				ftf6336_tpr_structure.y[i]=((uint16_t)(buf[2]&0X0F)<<8)+buf[3];
 				if((buf[0]&0XC0)!=0X80)
 				{
					ftf6336_tpr_structure.x[i]=ftf6336_tpr_structure.y[i]=0;//必须是contact事件，才认为有效	
					return;
				}
 			}
 		}
 		ftf6336_tpr_structure.touchsta.all |= FT6336_TP_PRES_DOWN;     //触摸按下标记
 	}
 	else
 	{
 		if(ftf6336_tpr_structure.touchsta.all & FT6336_TP_PRES_DOWN) 	//之前是被按下的
 			ftf6336_tpr_structure.touchsta.all &= ~0x80;        //触摸松开标记	
 		else
 		{
 			ftf6336_tpr_structure.x[0] = 0;
 			ftf6336_tpr_structure.y[0] = 0;
 			ftf6336_tpr_structure.touchsta.all &=0xe0;	//清楚触摸点有效标记
 		}
 	}
}
#endif
